export function host (url) {
  const host = url.replace(/^https?:\/\//, '').replace(/\/.*$/, '')
  const parts = host.split('.').slice(-3)
  if (parts[0] === 'www') parts.shift()
  return parts.join('.')
}

export function stripTags (input, allowed) {
  allowed = (((allowed || '') + '').toLowerCase().match(/<[a-z][a-z0-9]*>/g) || []).join('')
  const tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi
  const commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi
  return input.replace(commentsAndPhpTags, '').replace(tags, function ($0, $1) {
    return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : ''
  })
}

export function timeAgo (time) {
  const between = Date.now() / 1000 - Number(time)
  if (between < 60) {
    return '刚刚'
  } else if (between < 3600) {
    return pluralize(~~(between / 60), ' 分钟前')
  } else if (between < 86400) {
    return pluralize(~~(between / 3600), ' 小时前')
  } else {
    return pluralize(~~(between / 86400), ' 天前')
  }
}

export function formatTime (time, format) {
  const date = new Date(Number(time) * 1000)
  return format
  .toLowerCase()
  .replace('yy', date.getFullYear())
  .replace('mm', date.getMonth() + 1)
  .replace('dd', date.getDate())
  .replace('hh', date.getHours())
  .replace('ii', date.getMinutes())
  .replace('ss', date.getSeconds())
}

function pluralize (time, label) {
  return time + label
}
